<?php
/**
 * Created by PhpStorm.
 * User: Kevin
 * Date: 2018/9/10
 * Time: 18:20
 */
namespace app\admin\controller;
use think\Db;
use app\admin\model\OrderModel;
class Order extends Base
{
    /*
     * 订单首页数据
     */
    public function index(){
        if(request()->isAjax ()){
            extract(input());
            $map = [];
            if(isset($status)&&$status != ""){
                $map['order_status'] = $status;
            }
            if(isset($type)&&$type != ""){
                $map['order_type'] = $type;
            }
            if(isset($key)&&$key!="")
            {
                $map['user_username|order_number'] = ['like',"%" . $key . "%"];
            }
            if(isset($start)&&$start!=""&&isset($end)&&$end=="")
            {
                $map['order_time'] = ['>= time',$start];
            }
            if(isset($end)&&$end!=""&&isset($start)&&$start=="")
            {
                $map['order_time'] = ['<= time',$end];
            }
            if(isset($start)&&$start!=""&&isset($end)&&$end!="")
            {
                $map['order_time'] = ['between time',[$start,$end]];
            }
            $Nowpage = input('page') ? input('page'):1;
            $limits = input("limit")?input("limit"):10;// 获取总条数;
            $field=input('field');//字段
            $order=input('order');//排序方式
            if($field && $order){
                $od=$field." ".$order;
            }else{
                $od="order_time desc";
            }
            $user = new OrderModel();
            $count = $user->getAllCount($map);
            $lists = $user->getOrderData($map,$od, $Nowpage, $limits);
            return json(['code'=>0,'msg'=>'','count'=>$count,'data'=>$lists]);
        }
        return $this->fetch('order/index');
    }

    /*
     * 订单详情
     */
    public function orderDetail(){
        $id = input('id');
        $user = new OrderModel();
        $data = $user->getOrderDetail($id);
        return $this->fetch('order/order_detail',['data'=>$data]);
    }

    /*
     * 补充：
     * 退款订单处理：同意或拒绝退款
     * type:（3是同意，4是拒绝）
     * order_id，订单id
     */
    public function orderBackDoByAdmin(){
        extract(input());
        if($type != 3 && $type != 4){
            return json(['code'=>0,'msg'=>'传参错误！']);
        }
        $where = array();
        $where['order_id'] = $order_id;
        $update = array();
        $update['order_in_out'] = $type;
        Db::startTrans();
        try{
            $content = '您的退款申请被拒绝，如有疑问请联系店家';
            $content2 = '您的退款申请被拒绝';
            if($type == 3){
                $content = '您的退款申请已同意，请注意查收';
                $content2 = '您的退款申请已同意';
                $payinfo = Db::table('up_order')->where($where)
                    ->field('order_user_pay money,order_number number,order_back_transaction_id transaction_id,order_in_out,order_type,order_where,order_shopid')
                    ->find();
                if($payinfo['order_in_out'] == 3){
                    return json(['code'=>0,'msg'=>'请勿重复操作']);
                }
                $shopmoney = Db::table('up_shops')->where('s_id', $payinfo['order_shopid'])->value('s_money');
                if($payinfo['money'] > $shopmoney){
                    return json(['code'=>0,'msg'=>'商铺的余额不足,无法完成操作！']);
                }
                $tui1 = new \app\shop\controller\Base();
                if($payinfo['order_type'] == 1){
                    $back = $tui1->refund($payinfo);
                }else{
                    if($payinfo['order_where'] == 1){
                        $back = $tui1->refundWx($payinfo);
                    }else{
                        $tui = new \app\home\controller\Car();
                        $back = $tui->WeChatTuikuan($payinfo);
                    }
                }

                if($back['status'] != 200){
                    return json(['code'=>0,'msg'=>$back['msg']]);
                }else{
                    $order = Db::table('up_order')->where($where)
                        ->field('order_id,order_user_pay,order_shopid')
                        ->find();
                    $money = Db::table('up_shops')->where('s_id', $order['order_shopid'])->value('s_money');
                    $money -= $order['order_user_pay'];
                    Db::table('up_shops')
                        ->where('s_id', $order['order_shopid'])
                        ->update(['s_money'=>$money]);
                }
            }
            Db::table('up_order')->where($where)->update($update);
            Db::commit();
            //消息和推送
            $user = Db::table('up_order')->where($where)->value('order_userid');
            allInNotice('退款结果通知',$content,$user);
            sendJpush("$user", $content2, array('a' => 'b'), 2, 2, 1);
            return json(['code'=>200,'msg'=>'操作成功']);
        }catch (\Exception $e) {
            Db::rollback();
            return json(['code'=>100,'msg'=>'操作失败']);
        }
    }
    
}